package com.qq.client.view;
import java.awt.event.ActionEvent;
import com.qq.client.tools.*;
import java.io.*;
import java.sql.SQLException;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.qq.client.model.QqClientConServer;
import com.qq.common.*;

public class QqChat extends JFrame implements ActionListener{
    JTextField jtf;
    JTextArea jta;
    JButton jb;
    JPanel jp;
    String wodeId;
    String haoyou;
	public static void main(String[] args) {
		//QqChat qqchat=new QqChat("1");
	}
	public QqChat(String wodeId,String haoyou){
		this.wodeId=wodeId;
		this.haoyou=haoyou;
		jtf=new JTextField(15);
		jta=new JTextArea();
	    jb=new JButton("发送");
	    //监听发送按钮
	    jb.addActionListener(this);
	    jp=new JPanel();
	    jp.add(jtf);
	    jp.add(jb);
	    
	    this.add(jta,"Center");
	    this.add(jp,"South");
        this.setTitle(wodeId+"在和"+haoyou+"  聊天");
        this.setIconImage((new ImageIcon("image/qq.gif")).getImage());
        this.setSize(300,200);
        this.setVisible(true);
        
	}
	public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jb){
        Massage m=new Massage();
        m.setMesType(MessageType.message_comm_mes);
        m.setSender(this.wodeId);
        m.setGetter(this.haoyou);
        m.setCon(jtf.getText());
        jtf.setText("");
        //发送给服务器。
        try {
			ObjectOutputStream oos=new ObjectOutputStream
			(ZongThread.getClientConServerThread(wodeId).getS().getOutputStream());
		    oos.writeObject(m);
        } catch (Exception e1) {
			e1.printStackTrace();
		}
        
        }		
	}
   public void showMessage(Massage m){
	   String info=m.getSender()+"对你说:   "+m.getCon()+"\r\n";
		this.jta.append(info);
   }
	/*
	public void run() {
        while(true){
        	//读取
        try {
			ObjectInputStream ois=new ObjectInputStream(QqClientConServer.s.getInputStream());
			Massage m=(Massage)ois.readObject();
			String info=m.getSender()+"对你说:   "+m.getCon();
			this.jta.append(info);
       	} catch (IOException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
        }		
	}
	*/
}
